<?php

namespace controller;

/**
 * Description of AjaxBasket
 *
 * @author Damian
 */
class AjaxBasket extends Ajax{
    public function add(){
        $id=  \library\RequestMethods::post("id");
        $amount = \library\RequestMethods::post("amount");
        $session = \library\Registry::get("session");
        $shopURL = "moj_sklepik,";
        if ($id){
            $basket = ($session->get("basket"))?$session->get("basket"): new \library\Basket();
            /* @var $basket \library\Basket */
            $shopCriteria = new \model\Criteria();
            $shopCriteria->from(\model\TblShop::getModel()->getTable())
                    ->where(\model\TblShop::URL, "=", $shopURL);
            $shop = \model\TblShop::getModel()->first($shopCriteria);
            $productCriteria = \model\TblProduct::getModel()->createCriteria($shop->ShopID)
                    ->where(\model\TblProduct::ID, "=", $id,  \model\TblProduct::getModel()->getTable()," and ");
            $product = \model\TblProduct::getModel()->first($productCriteria);
            if ($product){
                if ($amount){
                    $basket->addMultiple($product, $amount);
                }
                else {
                    $basket->add($product);
                }
            }
            $session->set("basket",$basket);
        }
    }
    public function removeSingleElem(){
        $id=  \library\RequestMethods::post("id");
        $session = \library\Registry::get("session");
        $all=  \library\RequestMethods::post("all");
        $shopURL = "moj_sklepik,";
        if ($id){
            $basket = ($session->get("basket"))?$session->get("basket"): new \library\Basket();
            /* @var $basket \library\Basket */
            $shopCriteria = new \model\Criteria();
            $shopCriteria->from(\model\TblShop::getModel()->getTable())
                    ->where(\model\TblShop::URL, "=", $shopURL);
            $shop = \model\TblShop::getModel()->first($shopCriteria);
            $productCriteria = \model\TblProduct::getModel()->createCriteria($shop->ShopID)
                    ->where(\model\TblProduct::ID, "=", $id,  \model\TblProduct::getModel()->getTable()," and ");
            $product = \model\TblProduct::getModel()->first($productCriteria);
            if ($product){  
                $basket->removeSingleItem($product, $all);
            }
            $session->set("basket",$basket);
            var_dump($basket);
        }
    }
    public function clear(){
        $session = \library\Registry::get("session");
        $basket = ($session->get("basket"))?$session->get("basket"): new \library\Basket();
         /* @var $basket \library\Basket */
        $basket->clear();
        $session->set("basket",$basket);  
    }
    public function addPayment(){
        $session = \library\Registry::get("session");
        $order = ($session->get("order"))?$session->get("order"): new \model\TblOrder();
         /* @var $order  \model\TblOrder */
        $id=  \library\RequestMethods::post("id");
        $order->PaymentID=$id;
         $customer = $session->get("customer");
        if ($customer){
            $customer->DefaultPayment=$id;            
        }
        $session->set("customer",$customer);
        $session->set("order",$order);  
    }
    public function addDelivery(){
        $session = \library\Registry::get("session");
        $order = ($session->get("order"))?$session->get("order"): new \model\TblOrder();
         /* @var $order  \model\TblOrder */
        $id=  \library\RequestMethods::post("id");
        $order->DeliveryMethodID=$id;
        $customer = $session->get("customer");
        if ($customer){
            $customer->DefaultDelivery=$id;
        }
        $session->set("customer",$customer);
        $session->set("order",$order);  
    }
}
